package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.AdBo;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;

/**
 * 广告
 * Created by guilin on 16/5/11.
 */
public class AdAdapter extends PagerAdapter {

    private final Context context;
    private final ArrayList<AdBo> list;
    private final DrawableRequestBuilder<String> requestBuilder;
    private final RecyclerViewItemOnClickListener<AdBo> itemOnClickListener;

    public AdAdapter(Context context, ArrayList<AdBo> list, DrawableRequestBuilder<String> requestBuilder,
                     RecyclerViewItemOnClickListener<AdBo> itemOnClickListener) {
        this.context = context;
        this.list = list;
        this.requestBuilder = requestBuilder;
        this.itemOnClickListener = itemOnClickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        ImageView image = new ImageView(context);
        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        final AdBo adBo = list.get(position);
        GlideLoad.load(new GlideLoad.Builder(requestBuilder, adBo.getImageUrl())
                .placeHolder(R.drawable.ic_banner)
                .error(R.drawable.ic_banner)
                .openWifiSwitch()
                .into(image));
        image.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(adBo, 0);
            }
        });
        container.addView(image, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        return image;
    }
}
